package com.hotelsystem.reservations.layouts;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;

import com.hotelsystem.reservation.controllers.RoomListController;
import com.hotelsystem.reservations.rooms.Room;

public class RoomListView {
	private JList<String> roomsList = new JList<String>();
	private JButton open, delete;
	private JPanel mainPanel;

	public RoomListView() {
		buildUI();
	}
	
	public void setRoomsList(ArrayList<Room> rooms) {
		DefaultListModel<String> roomModels = new DefaultListModel<>();
		for(Room room : rooms) {
			roomModels.addElement(room.getRoomNumber() + " " + room.getRoomType());
			System.out.println(room.getRoomNumber() + " " + room.getRoomType());
		}
		this.roomsList.setModel(roomModels);
	}

	public JList<String> getRoomsList() {
		return roomsList;
	}

	public void setRoomsList(JList<String> roomsList) {
		this.roomsList = roomsList;
	}

	public JPanel getMainPanel() {
		return mainPanel;
	}

	public JButton getOpen() {
		return open;
	}

	public JButton getDelete() {
		return delete;
	}

	public void addListenerForOpen(ActionListener action) {
		open.addActionListener(action);
	}

	public void addListenerForDelete(ActionListener action) {
		delete.addActionListener(action);
	}

	private void buildUI() {
		mainPanel = new JPanel(new BorderLayout());
		//roomsList = new JList<String>();
		mainPanel.add(roomsList, BorderLayout.CENTER);
		mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

		JPanel buttonsPanel = new JPanel(new FlowLayout());
		buttonsPanel = buildButtons(buttonsPanel);

		mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
	}

	private JPanel buildButtons(JPanel buttonsPanel) {
		delete = new JButton("Delete");
		open = new JButton("Open");
		
		buttonsPanel.add(open);
		buttonsPanel.add(delete);
		buttonsPanel.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));
		
		return buttonsPanel;
	}

	public static void main(String[] args) {
		RoomListController controller = new RoomListController();

		controller.fillList();

		JFrame frame = new JFrame();
		JPanel panel = controller.getRoomListView().getMainPanel();
		frame.add(panel);
		frame.setTitle("Rooms");
		frame.setSize(400, 200);
		frame.setVisible(true);
	}

}
